<template>
  <div class="HomeMv w">
    <!-- 选择参数 -->
    <div class="query">
      <div>
        <span
          :class="item == queryInfo.area ? 'active' : null"
          v-for="(item, index) in $constant.Mv.MvArea"
          :key="index"
          @click="queryInfoChange('area', item)"
        >
          {{ item }}
        </span>
      </div>
      <div>
        <span
          :class="item == queryInfo.type ? 'active' : null"
          v-for="(item, index) in $constant.Mv.MvType"
          :key="index"
          @click="queryInfoChange('type', item)"
        >
          {{ item }}
        </span>
      </div>
      <div>
        <span
          :class="item == queryInfo.order ? 'active' : null"
          v-for="(item, index) in $constant.Mv.MvOrder"
          :key="index"
          @click="queryInfoChange('order', item)"
        >
          {{ item }}
        </span>
      </div>
    </div>
    <GlobalMvItem
      :list="mvList"
      class="globalMvItem"
      @mvItemClick="mvItemClickEmit"
    />
    <!-- 分页 -->
    <!-- 分页 -->
    <el-row type="flex" class="row-bg" justify="center">
      <el-pagination
        layout="prev, pager, next"
        :total="total"
        :page-size="20"
        @current-change="paginationCurrentChange"
        :current-page="currentPage"
      >
      </el-pagination>
    </el-row>
  </div>
</template>

<script>
import { getMvList } from "../../../../api/mvApi";
export default {
  name: "HomeMv",
  data() {
    return {
      queryInfo: {
        area: "全部",
        order: "最热",
        type: "全部",
        limit: 20,
        offset: 0,
      },
      mvList: [],
      total: 0,
      currentPage: 1,
    };
  },
  created() {
    this.getMvListMethods();
  },
  methods: {
    async getMvListMethods() {
      try {
        const result = await getMvList(this.queryInfo);
        if (result.code === 200) {
          this.mvList = result.data;
          if (result.count) {
            this.total = result.count;
          }
        }
      } catch (error) {}
    },
    queryInfoChange(flag, value) {
      console.log(flag);
      switch (flag) {
        case "area":
          this.queryInfo.area = value;
          break;
        case "type":
          this.queryInfo.type = value;
          break;
        case "order":
          this.queryInfo.order = value;
          break;
      }

      this.getMvListMethods();
      this.currentPage = 1;
      this.queryInfo.offset = 0;
    },
    // 页码切换
    paginationCurrentChange(newPage) {
      this.$utils.scrollTop(
        document.querySelector(".globalMvItem").offsetTop,
        200
      );
      // this.mvList = [];
      this.queryInfo.offset = (newPage - 1) * this.queryInfo.limit;
      this.currentPage = newPage;
      this.getMvListMethods();
    },
    // mvItemClickEmit   mvItem 组件的点击事件
    mvItemClickEmit(id) {
      // console.log(id);
      this.$router.push(`/mvDetails/${id}`);
    },
  },
};
</script>

<style lang="less" scoped>
.HomeMv {
  /deep/ .el-pager li.active {
    color: #fff;
    background: @globalColor;
    border-radius: 50%;
    &:hover {
      color: #fff;
    }
  }
  /deep/ .el-pager li:hover {
    color: @globalColor;
  }

  padding: 30px 0;
  font-size: 14px;
  .query {
    font-size: 16px;
    color: #333;
    padding-bottom: 20px;
    span {
      display: inline-block;
      padding: 2px 8px;
      margin-right: 5px;
      border-radius: 5px;
      cursor: pointer;
      &:nth-child(1) {
        margin-right: 20px;
      }
      &.active {
        background-color: @globalColor;
        color: #fff;
      }
    }
    div {
      padding-bottom: 15px;
    }
  }
}
</style>
